package com.xingzhe.pengji.security.controller.system;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xingzhe.pengji.framework.controller.PaginationController;
import com.xingzhe.pengji.security.domain.User;
import com.xingzhe.pengji.security.services.UserService;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController extends PaginationController
{
    /**
     * 用户
     */
    private User user;
    
    /**
     * 用户列表
     */
    private List<User> usersList;
    
    
    private ModelAndView modelAndView;
    
    @Autowired
    private UserService<User> userService;
    
    private String operatorManString = "Admin";
    
    private final String USER_INDEX = "redirect:/user/index.html";
    
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);
    
    /**
     * 第一次加载时所对应的请求
     */
    @Override
    protected ModelAndView executePaginationController(HttpServletRequest request, HttpServletResponse response,
            int pageNumber, int pageSize) throws Exception
    {
        usersList = userService.queryForPageUserList(null, "id", pageSize, pageNumber, false);
        modelAndView = new ModelAndView();
        modelAndView.addObject("usersList", usersList);
        modelAndView.setViewName("/jsp/user/queryPortalUser");
        return modelAndView;
    }
    
    @Override
    protected int getItemCount(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        return userService.getItemCount(user);
    }
    
    /**
     * 添加的跳转
     * @param  
     * @return url 地址
     */
    @RequestMapping(method = RequestMethod.GET, value = "/add.html")
    public String addUser()
    {
        return "/jsp/user/addUser";
    }
    
    /**
     * 新增User 用户
     * @param user
     * @return
     */
    @RequestMapping(value = "/save.html")
    public ModelAndView saveUser(@ModelAttribute("user") User user)
    {
        user.setCreateTime(new Date());
        user.setCreator(operatorManString);
        user.setUpdateTime(new Date());
        user.setUpdateMan(operatorManString);
        logger.debug(user.toString());
        userService.saveUser(user);
        return null;
    }
    
    /**
     * 保存修改后的用户
     * @param user
     * @return
     */
    @RequestMapping(value = "/saveOrUpdate.html")
    public String saveOrUpdateUser(@ModelAttribute("user") User user)
    {
        user.setUpdateTime(new Date());
        user.setUpdateMan(operatorManString);
        userService.updateUser(user);
        return USER_INDEX;
    }
    
    /**
     * 将需要更新的用户带至需要修改的页面
     * @param userIds
     * @return
     */
    @RequestMapping(value = "/update.html")
    public ModelAndView updateUser(@RequestParam("userIds") String userIds)
    {
        String[] stringargs = userIds.split(",");
        user = new User();
        user.setId(Integer.valueOf(stringargs[0]));
        user = userService.getUserById(user);
        modelAndView = new ModelAndView();
        modelAndView.addObject("user", user);
        modelAndView.setViewName("/jsp/user/updateUser");
        
        return modelAndView;
    }
    
    /**
     * 删除该用户
     * @param userIds
     * @return
     */
    @RequestMapping(value = "/delete.html")
    public String delUser(@RequestParam("userIds") String userIds)
    {
        
        String[] argsString = userIds.split(",");
        user = new User();
        for ( String string : argsString )
        {
            user.setId(Integer.valueOf(string));
            user.setUpdateTime(new Date());
            userService.delUser(user);
        }
        return USER_INDEX;
    }
    
    /**
     * 验证该用户名是否存在
     * @param account
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/validateAccount.html")
    public Object validateAccount(@RequestParam("account") String account)
    {
        user = new User();
        user.setAccount(account);
        user = userService.getUserByAccount(user);
        if ( null == user )
        {
            return true;
        }
        return false;
    }
}
